package medium最小栈;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MinStack {

    List<Integer> list ;
    int min ;

    int size;

    int top;

    public MinStack() {
        list = new ArrayList<>();
        min = Integer.MAX_VALUE;
        size = 0;
    }

    public void push(int val) {
        list.add(val);
        size++;
        if (val < min)
            min = val;
        top = val;
    }

    public void pop() {
        if (size != 0){
            list.remove(size-1);
            size--;
            if (size!=0){
                top = list.get(size-1);
                min = Collections.min(list);
            }
            else {
                min = Integer.MAX_VALUE;
            }
        }
    }

    public int top() {
        return top;
    }

    public int getMin() {
        return min;
    }
}
